<?php
/**
 * Bancha Project : Combining Ext JS and CakePHP (http://banchaproject.org)
 * Copyright 2011-2012 StudioQ OG
 *
 * @package       Bancha
 * @category      tests
 * @copyright     Copyright 2011-2012 StudioQ OG
 * @link          http://banchaproject.org Bancha Project
 * @since         Bancha v 0.9.0
 * @author        Florian Eckerstorfer <f.eckerstorfer@gmail.com>
 * @author        Roland Schuetz <mail@rolandschuetz.at>
 */

App::uses('BanchaResponseTransformer', 'Bancha.Bancha/Network');

/**
 * BanchaResponseTransformerTest
 *
 * @package       Bancha
 * @category      tests
 */
class BanchaResponseTransformerTest extends CakeTestCase {

/**
 * Tests the transform() method for multiple return records
 *
 */
	public function testTransformSingleRecord() {
		// Response generated by CakePHP
		$cakeResponse = array(
			array(
				'Article'	=> array(
					'id'	=> 304,
					'title'	=> 'foo',
				),
			),
			array(
				'Article'	=> array(
					'id'	=> 305,
					'title'	=> 'bar',
				),
			)
		);

		$request = new CakeRequest();
		$request->addParams(array(
			'controller'	=> 'Articles',
			'action'		=> 'index',
		));

		// Response expected by Ext JS (in JSON).
		$expectedResponse = array(
			'success' => true,
			'data' => array(
				array(
					'id'	=> 304,
					'title'	=> 'foo',
				),
				array(
					'id'	=> 305,
					'title'	=> 'bar',
				),
			),
		);

		$this->assertEquals($expectedResponse, BanchaResponseTransformer::transform($cakeResponse, $request));
	}

/**
 * Tests the transform() method for a single returned record
 *
 */
	public function testTransformMultipleRecords() {
		// Response generated by CakePHP.
		$cakeResponse = array(
			'Article'	=> array(
				'id'	=> 304,
				'title'	=> 'foo',
			),
		);

		$request = new CakeRequest();
		$request->addParams(array(
			'controller'	=> 'Articles',
			'action'			=> 'view'
		));

		// Response expected by Ext JS (in JSON).
		$expectedResponse = array(
			'success' => true,
			'data' => array(
				'id'	=> 304,
				'title'	=> 'foo',
			),
		);

		$this->assertEquals($expectedResponse, BanchaResponseTransformer::transform($cakeResponse, $request));
	}

	
/**
 * Bancha also understand simple true/false values
 *
 * @dataProvider getTrueFalse
 */
	public function testTransformPrimitives($primitiveResult) {
		// Response generated by CakePHP.
		$cakeResponse = $primitiveResult;

		$request = new CakeRequest();
		$request->addParams(array(
			'controller'	=> 'Users',
			'action'		=> 'delete'
		));

		// Response expected by Ext JS (in JSON).
		$expectedResponse = array(
			'success' => $primitiveResult,
		);

		$this->assertEquals($expectedResponse, BanchaResponseTransformer::transform($cakeResponse, $request));
	}
	// data provider
	public function getTrueFalse() {
		return array(array(true),array(false));
	}
	
/**
 * Bancha understands cake responses with pagination data
 * @param $paginatedRecords cake response to transform
 * @param $expectedResponse Response expected by Ext JS (in JSON).
 *
 * @dataProvider getCakeRecords
 */
	public function testTransformPaginated($paginatedRecords,$expectedResponse) {
		$request = new CakeRequest();
		$request->addParams(array(
			'controller'	=> 'Articles',
			'action'		=> 'delete'
		));

		$this->assertEquals($expectedResponse, BanchaResponseTransformer::transform($paginatedRecords, $request));
	}
	
	// data provider
	public function getCakeRecords() {
		return array(
			array( array('count'=>0,'records'=>array()),  					array('success'=>true,'total'=>0,'data'=>array())   ),
			array( array('count'=>9,'records'=>array(
				array('Article'=>array('id'=>5,'name'=>'whatever')),
				array('Article'=>array('id'=>6,'name'=>'whatever2')))),     array('success'=>true,'total'=>9,'data'=>array(
																						array('id'=>5,'name'=>'whatever'),
																						array('id'=>6,'name'=>'whatever2')))    ),
		);
	}

/**
 * Unrecognizable structures are just passed through
 * @param $cakeResponse cake response to transform
 * 
 * @dataProvider getUnrecognizableResponses
 */
	public function testTransformUnrecognizable($cakeResponse) {
		$request = new CakeRequest();
		$request->addParams(array(
			'controller'	=> 'Articles',
			'action'		=> 'delete'
		));

		$this->assertEquals($cakeResponse, BanchaResponseTransformer::transform($cakeResponse, $request));
	}

	// data provider
	public function getUnrecognizableResponses() {
		return array(
			array( array('msg'=>'lala') ),
			array( array('sucess'=>'true') ), // already in ext(-like) structure
			array( array('data'=>array()) ), // already in ext(-like) structure
			array( array('sucess'=>'true','data'=>array()) ), // already in ext(-like) structure
		);
	}
}
